<?php

/**
 * import actions.
 *
 * @package    raidpoints
 * @subpackage import
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class syncActions extends sfActions
{
    /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex(sfWebRequest $request)
    {
        $q = Doctrine::getTable("DataSyncStatus")
            ->createQuery()->orderBy("date desc")->limit(1);
        $r = $q->execute();

        $this->index = true;
        if ($r->count() == 0 || $r->getFirst()->getStatus() == "Ready" || $r->getFirst()->getStatus() == "Finished")
            $this->index = false;
    }

    public function executeView(sfWebRequest $request)
    {
        $this->setTemplate('upload');
    }

    public function executeSync(sfWebRequest $request)
    {
        //$this->forward404Unless($request->isMethod(sfRequest::POST));

        //shell_exec($commandString);
//        $WshShell = new COM("WScript.Shell");
//        $this->upload .= $oExec = $WshShell->run($commandString, 7, false);
        //$this->setTemplate('show');

        $q = Doctrine::getTable("DataSyncStatus")
            ->createQuery()->orderBy("date desc")->limit(1);
        $r = $q->execute();
        if ($r->count() == 1)
        {
            $date = strtotime($r->getFirst()->getDate());
            if ( (time() - $date) > 60 )
            {
				$bg = new BackgroundIt();
				$bg->run(sfConfig::get('sf_root_dir').'/symfony raidpoints:armory-sync', sfConfig::get("app_path_to_php"));			
                // $WshShell = new COM("WScript.Shell");
                // $cmd = 'php\php.exe '.sfConfig::get('sf_root_dir').'/symfony raidpoints:armory-sync';
                // $oExec = $WshShell->Run($cmd, 7, false);
            }
        }
        else
        {
			$bg = new BackgroundIt();
			$bg->run(sfConfig::get('sf_root_dir').'/symfony raidpoints:armory-sync', sfConfig::get("app_path_to_php"));		
        }
        //$this->setTemplate('upload');
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->getResponse()->setContentType('plain/text');

        $q = Doctrine::getTable("DataSync")
            ->createQuery()->select("COUNT(*)");
        $r = $q->execute();
        $count = $r->getFirst()->toArray();
        $queue = $count["COUNT"];

        if ($queue > 0)
        {
            $q = Doctrine::getTable("DataSyncStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count = $r->getFirst()->toArray();
            $count = $count["COUNT"];
            sleep(15);
            $q = Doctrine::getTable("DataSyncStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count2 = $r->getFirst()->toArray();
            $count2 = $count2["COUNT"];
            if ($queue > 0 && $count == $count2)
            {
                $bg = new BackgroundIt();
                $bg->run(sfConfig::get('sf_root_dir').'/symfony raidpoints:armory-run', sfConfig::get("app_path_to_php"));
            }
        }

        $q = Doctrine::getTable("DataSyncStatus")
            ->createQuery()
            ->orderBy("date desc")
            ->limit(20);
        $r = $q->execute();

        $table = <<<EOF
        <table cellspacing="0">
            <thead class="ui-widget-header">
                <tr>
                    <th class="sf_admin_text sf_admin_list_th_id ui-state-default ui-th-column">Date</th>
                    <th class="sf_admin_text sf_admin_list_th_id ui-state-default ui-th-column">Status</th>
                    <th class="sf_admin_text sf_admin_list_th_id ui-state-default ui-th-column">Log</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th colspan="3">
                        <div class="ui-state-default ui-th-column ui-corner-bottom">
                            <div class="sf_admin_pagination" id="sf_admin_pager">
                            </div>
                        </div>
                    </th>
                </tr>
            </tfoot>
            <tbody>
EOF;
        foreach ($r as $import)
        {
            $table .= "<tr class=\"sf_admin_row ui-widget-content\"><td>".$import->getDate()."</td><td>".$import->getStatus()."</td><td>".$import->getText()."</td></tr>";
        }
        $table .= "</tbody></table>";

        return $this->renderText($table);
    }

    public function executeProgressbar(sfWebRequest $request)
    {
        $this->getResponse()->setContentType('plain/text');

        $q = Doctrine::getTable("DataSyncStatus")
            ->createQuery()
            ->orderBy("date desc")
            ->limit(1);
        $r = $q->execute();

        $percent = 0;
        if ($r->count() == 1)
        {
            if ($r->getFirst()->getStatustotal() > 0)
            $percent = ($r->getFirst()->getStatusbar() * 1.0) / $r->getFirst()->getStatustotal();
        }
        return $this->renderText($percent);
    }

}
